package com.example.qt.ptplatform.controller;

import com.example.qt.ptplatform.entity.vo.CommentFavoriteVO;
import com.example.qt.ptplatform.service.CommentFavoriteService;
import com.example.qt.ptplatform.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comments/favorites")
public class CommentFavoriteController {

    @Autowired
    private CommentFavoriteService commentFavoriteService;

    /**
     * 收藏评论
     * POST /comments/favorites?userId=1&commentId=2
     */
    @PostMapping
    public JsonResult favoriteComment(
            @RequestParam("userId") Integer userId,
            @RequestParam("commentId") Integer commentId) {
        try {
            if (userId == null || commentId == null) {
                return JsonResult.error("参数不能为空");
            }

            boolean result = commentFavoriteService.favoriteComment(userId, commentId);
            if (result) {
                Map<String, Object> data = new HashMap<>();
                data.put("userId", userId);
                data.put("commentId", commentId);
                return JsonResult.success("收藏成功", data);
            } else {
                return JsonResult.error("收藏失败，可能已收藏");
            }
        } catch (Exception e) {
            return JsonResult.error("收藏失败: " + e.getMessage());
        }
    }

    /**
     * 取消收藏评论
     * DELETE /comments/favorites?userId=1&commentId=2
     */
    @DeleteMapping
    public JsonResult unfavoriteComment(
            @RequestParam("userId") Integer userId,
            @RequestParam("commentId") Integer commentId) {
        try {
            if (userId == null || commentId == null) {
                return JsonResult.error("参数不能为空");
            }

            boolean result = commentFavoriteService.unfavoriteComment(userId, commentId);
            if (result) {
                Map<String, Object> data = new HashMap<>();
                data.put("userId", userId);
                data.put("commentId", commentId);
                return JsonResult.success("取消收藏成功", data);
            } else {
                return JsonResult.error("取消收藏失败，可能未收藏");
            }
        } catch (Exception e) {
            return JsonResult.error("取消收藏失败: " + e.getMessage());
        }
    }

    /**
     * 检查是否已收藏
     * GET /comments/favorites/check?userId=1&commentId=2
     */
    @GetMapping("/check")
    public JsonResult checkFavoriteStatus(
            @RequestParam("userId") Integer userId,
            @RequestParam("commentId") Integer commentId) {
        try {
            if (userId == null || commentId == null) {
                return JsonResult.error("参数不能为空");
            }

            boolean isFavorite = commentFavoriteService.isFavorite(userId, commentId);
            Map<String, Object> data = new HashMap<>();
            data.put("isFavorite", isFavorite);
            data.put("userId", userId);
            data.put("commentId", commentId);
            return JsonResult.success("查询成功", data);
        } catch (Exception e) {
            return JsonResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户收藏的评论总数
     * GET /comments/favorites/count?userId=1
     */
    @GetMapping("/count")
    public JsonResult getFavoriteCount(
            @RequestParam("userId") Integer userId) {
        try {
            if (userId == null) {
                return JsonResult.error("用户ID不能为空");
            }

            int count = commentFavoriteService.getFavoriteCountByUser(userId);
            Map<String, Object> data = new HashMap<>();
            data.put("userId", userId);
            data.put("favoriteCount", count);
            return JsonResult.success("查询成功", data);
        } catch (Exception e) {
            return JsonResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户收藏的评论列表
     * GET /comments/favorites/list?userId=1
     */
    @GetMapping("/list")
    public JsonResult getUserFavoriteComments(
            @RequestParam("userId") Integer userId) {
        try {
            if (userId == null) {
                return JsonResult.error("用户ID不能为空");
            }

            List<CommentFavoriteVO> favoriteComments = commentFavoriteService.getUserFavoriteComments(userId);
            int count = commentFavoriteService.getFavoriteCountByUser(userId);

            Map<String, Object> data = new HashMap<>();
            data.put("userId", userId);
            data.put("favoriteComments", favoriteComments);
            data.put("total", count);

            return JsonResult.success("获取收藏列表成功", data);
        } catch (Exception e) {
            return JsonResult.error("获取收藏列表失败: " + e.getMessage());
        }
    }
}